# Import dependencies
import pandas as pd
import hvplot.pandas
# Function for combining graphs
def overlay(df, df2):
return df * df2
# Import Constant Maturity Treasury Rate
treasuries = pd.read_csv(
"constant_maturity_treasury_rates.csv",
index_col="Date",
parse_dates=True,
infer_datetime_format=True
)
# Import Historic Gold Prices
gold = pd.read_csv(
"goldx.csv",
index_col="Date",
parse_dates=True,
infer_datetime_format=True
)
# Import Current Gold Prices
gold_current = pd.read_csv(
"current_gold.csv",
index_col="Date",
parse_dates=True,
infer_datetime_format=True
)
# Import Real GDP
gdp = pd.read_csv(
"real_gdp.csv",
index_col="Date",
parse_dates=True,
infer_datetime_format=True
)
# Rename GDP Columns and get percent change of DataFrame
gdp = gdp.rename(columns={"GDPC1":"Billions of Dollars"})
gdp = gdp.pct_change()
# Clean data
treasuries = treasuries.fillna(0)
# Replace strings
treasuries = treasuries.replace(to_replace="ND", value=0)
# Convert values to float
treasuries = treasuries.astype(float)
# Create variables for recent dates
today = treasuries.iloc[-1,:]
fifty_days = treasuries.iloc[-50,:]
half_year = treasuries.iloc[-182,:]
last_year = treasuries.iloc[-365,:]
# Create the DataFrame
recent_df = pd.concat([today, fifty_days, half_year, last_year], axis=1)
recent_df.columns = ['Today', 'Fifty Days Ago', 'Half a Year Ago', 'Last Year']
# Plot the data
line_plot = recent_df.hvplot(title = "Treasury Yield Curve, %", grid=True, xlabel="Bond Maturity", ylabel="Yield (%)")
scatter_plot = recent_df.hvplot.scatter()
overlay(line_plot, scatter_plot)
last_year.hvplot(title="Treasury Yield Curve, 2021")
today.hvplot(title="Treasury Yield Curve, 2022")
# Plot recent GDP
recent_gdp = gdp[289:302].hvplot(title="Real GDP 2019-04-01 to 2022-04-01", ylabel="Percent Change")
display(recent_gdp)
# Plot 2019 yield curve
recent_treasuries = treasuries.iloc[14999,:].hvplot(title="Yield Curve, 2019-07-01", xlabel="Maturity", ylabel="Rate")
display(recent_treasuries)
# Slice the GDP DataFrame from 1996-2001 and create the plot
dotcom_gdp = gdp[213:227].hvplot(title="Real GDP, 2000-01-01 to 2003-07-01", ylabel="Percent Change")
# Slice the gold DataFrame and create the plot
dotcom_gold = gold[4474:5224].hvplot(title="Gold Spot Price, 1998-01-04 to 2001-01-02")
# Remove an empty column for data not available before 2000
treasuries_1 = treasuries.drop(columns="1-month")
# Slice the DataFrame and create plot
treasury_1999 = treasuries_1.iloc[9665,:].hvplot(title="Yield Curve, 1999-01-01")
# Plot all dataframes
display(treasury_1999)
display(dotcom_gdp)
display(dotcom_gold["High"])
# Slice the GDP DataFrame from 1996-2001 and create the plot
subprime_gdp = gdp[237:251].hvplot(title="Real GDP, 2006-01-01 to 2009-07-01", ylabel="Percent Chaange")
# Slice the gold DataFrame and create the plot
subprime_gold = gold[2474:2979].hvplot(title="Gold Spot Price, 2007-01-02 to 2009-01-02")
# Slice the DataFrame and create plot
treasury_2007 = treasuries.iloc[11740,:].hvplot(title="Yield Curve, 2007-01-01")
# Plot all dataframes
display(treasury_2007)
display(subprime_gdp)
display(subprime_gold["High"])
# Slice the GDP DataFrame from 1996-2001 and create the plot
eighties_gdp = gdp[132:149].hvplot(title="Real GDP, 1979-01-01 to 1984-01-01", ylabel="Percent Change")
# Slice the gold DataFrame and create the plot
eighties_gold = gold[8759:9768].hvplot(title="Gold Spot Price, 1980-01-02 to 1984-01-03")
# Slice the DataFrame and create plot
treasuries_1 = treasuries.drop(columns=["1-month", "3-month", "6-month"])
treasury_1979 = treasuries_1.iloc[4436,:].hvplot(title="Yield Curve, 1979-01-02")
# Plot all dataframes
display(treasury_1979)
display(eighties_gdp)
display(eighties_gold["High"])
# Slice the GDP DataFrame from 1996-2001 and create the plot
covid_gdp = gdp[285:302].hvplot(title="Real GDP, 2018-01-01 to 2022-04-01", ylabel="Percent Change")
# Slice the gold DataFrame and create the plot
covid_gold = gold_current["Close/Last"].hvplot(title="Gold Spot Price, 2017-10-06 to 2022-10-06")
# Slice the DataFrame and create plot
treasury_2019 = treasuries.iloc[14872,:].hvplot(title="Yield Curve, 2019-01-02")
treasury_2021 = treasuries.iloc[15395,:].hvplot(title="Yield Curve, 2021-01-04")
# Plot all dataframes
display(treasury_2019)
display(treasury_2021)
display(covid_gdp)
display(covid_gold)